var jhdw = {
    setBtnStatus:function () {
    }
}

$(function () {

    $("#_jhdw").qyjgzcombobox();
    $("#jhdw").qyjgzcombobox();
    //表格初始加载
    $('#grid_jhdw').datagrid({
        url:$.webPath() + "/organ/jhdw/jhdwList",
        toolbar:"#toolbar_jhdw",
        fitColumns:true,
        singleSelect:true,
        pagination:true,
        pageNumber: 1,
        fit:true,
        rownumbers:true,
        striped:true,
        columns:[[
            {field:'qc', title:'全称', width:50},
            {field:'jc', title:'简称', width:30},
            {field:'qyfr_qyqc', title:'企业法人', width:30},
            {field:'lxr', title:'联系人', width:30},
            {field:'bm', title:'编码', width:30},
            {field:'bz', title:'备注', width:60},
            {field:'sfzx', title:'启用', align:'center', width:10, formatter:function (value, row, index) {
                if (value == '是')
                    return '否';
                else
                    return '是';
            }}
        ]],
        onLoadSuccess:function(){
            $("#btn_jhdw_mod").hide();
            $("#btn_jhdw_del").hide();
            $("#qyjg_start").hide();
            jhdw.setBtnStatus();
        },
        onClickRow:function(rowIndex, rowData){
            $("#btn_jhdw_mod").show();
            $("#btn_jhdw_del").show();
            if (rowData.sfzx == null || rowData.sfzx == '是') {
                $("#btn_jhdw_start").text('启用');
                $("#btn_jhdw_del").show();
                $("#btn_jhdw_start").removeClass('stop').addClass('start');
                $('#btn_jhdw_start').unbind('click');
                $('#btn_jhdw_start').bind('click', function () {
                    changeJhdwState($.webPath() + '/organ/jhdw/start_jhdw', '确认启用此计划单位？');
                });
            } else {
                $("#btn_jhdw_start").text('停用');
                $("#btn_jhdw_del").hide();
                $("#btn_jhdw_start").removeClass('start').addClass('stop');
                $('#btn_jhdw_start').unbind('click');
                $('#btn_jhdw_start').bind('click', function () {
                    changeJhdwState($.webPath() + '/organ/jhdw/stop_jhdw', '确认停用此计划单位？');
                });
            }
            $("#btn_jhdw_start").show();
            jhdw.setBtnStatus();
        }
    });
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_jhdw_add").click(function () {
        openAddjhdwDialog('新增计划单位', 'add_jhdw');
    });
    //修改按钮
    $("#btn_jhdw_mod").click(function () {
        openModifyjhdwDialog('修改计划单位', 'update_jhdw');
    });
    //删除按钮
    $("#btn_jhdw_del").click(function () {
        jhdw_destroyItem();
    });
    //查询按钮
    $("#btn_jhdw_search").click(function () {
        jhdw_search()
    });
    //启用按钮触发
    $("#btn_jhdw_start").click(function () {
        changeJhdwState();
    });


    //双击事件
    $("#grid_jhdw").datagrid({
        onDblClickCell:function (index, field, value) {
            if (field == "qc") {
                openModifyjhdwDialog('修改计划单位', 'update_jhdw');
            }
        } ,
        rowStyler:function(index,row){
            if(row.sfzx == '是') {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        }
    });
});

//新增计划单位
function openAddjhdwDialog(title, action) {
    var href = $.webPath() + "/organ/jhdw/jhdw_content";
    var actionUrl = $.webPath() + "/organ/jhdw/" + action;
    showjhdwDialog(title, href, actionUrl);
}

//修改计划单位
function openModifyjhdwDialog(title, action) {
    var record = $("#grid_jhdw").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录！", "warning");
    }
    var href = $.webPath() + "/organ/jhdw/jhdw_content?jhdwId=" + record.id;
    var actionUrl = $.webPath() + "/organ/jhdw/" + action;
    showjhdwDialog(title, href, actionUrl);
    return;
}

//弹出新增或修改的窗口
function showjhdwDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:450,
        cache:false,
        iconCls:'icon-edit',
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    //$.messager.progress();
                    $(win).mask();
                    $('#form_jhdw').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                //$.messager.progress('close');
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            //$.messager.progress('close');
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_jhdw').datagrid('reload');
                                //$('#_jhdw').combotree('clear');
                               // $('#jhdw').combotree('clear');
                               // $("#_jhdw").combotree("reload", $.webPath() + "/organ/qyjg/qyjg_tree?type=0");
                               // $("#jhdw").combotree("reload", $.webPath() + "/organ/qyjg/qyjgjc_tree?type=0");
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除载具
function jhdw_destroyItem() {
    var row = $('#grid_jhdw').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该计划单位？', function (r) {
            if (r) {
                var index = $('#grid_jhdw').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/organ/jhdw/delete_jhdw",
                    dataType: 'json',
                    type: 'POST',
                    data: {jhdwId:row.id},
                    beforeSend: function () {
                        $("#layout_jhdw").mask();
                    },
                    success:function(data){
                        $("#layout_jhdw").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_jhdw').datagrid('deleteRow', index);
                            $('#grid_jhdw').datagrid('reload');
                          //  $('#_jhdw').combotree('clear');
                          //  $('#jhdw').combotree('clear');
                          //  $("#_jhdw").combotree("reload", $.webPath() + "/organ/qyjg/qyjg_tree?type=0");
                          //  $("#jhdw").combotree("reload", $.webPath() + "/organ/qyjg/qyjgjc_tree?type=0");
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else{
                            $.messager.alert("提示", data.message, "info");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的计划单位！", "warning");
    }
}
//改变状态，启用停用
function changeJhdwState(url, message) {
    var jhdwId = $("#grid_jhdw").datagrid("getSelected").id;
    if (jhdwId) {
        $.messager.confirm("确认", message, function (r) {
            if (r) {
                $.ajax({
                    url:url + "?jhdwId=" + jhdwId,
                    type:"json",
                    success:function (data) {
                        if (data.statusCode == 200) {
                            $('#grid_jhdw').datagrid('reload');
                            $.messager.show({
                                title:'提示框',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $.messager.alert('提示', data.message, 'info');
                        }
                    }
                });
            }
        })
    }
}

//查询计划单位
function jhdw_search() {
 //   var type = $("#qyjg_toolbar input[name='query_type']:checked").val();
    var qc = $('#_jhdw').attr("_text") == null ? null : $('#_jhdw').attr("_text");
    var jc = $('#jhdw').attr("_text") == null ? null : $('#jhdw').attr("_text");
    var options = $.extend(getJhdwGridOption(),
        {queryParams:{ qc:qc,jc:jc}});
    $('#grid_jhdw').datagrid(options);
}
function getJhdwGridOption() {
    var options = {
        url:$.webPath() + "/organ/jhdw/jhdwList",
        toolbar: "#toolbar_jhdw",
        fitColumns: true,
        singleSelect: true,
        pageNumber:1,
        pagination: true,
        fit: true,
        rownumbers: true
       /* onLoadSuccess:function(){
            $("#btn_jhdw_mod").hide();
            $("#btn_jhdw_del").hide();
            $("#btn_jhdw_start").hide();
            jhdw.setBtnStatus();
        },
        onClickRow:function(rowIndex, rowData){
            $("#btn_jhdw_mod").show();
            $("#btn_jhdw_del").show();
            if (rowData.sfzx == null || rowData.sfzx == '是') {
                $("#btn_jhdw_start").text('启用');
                $("#btn_jhdw_start").removeClass('stop').addClass('start');
                $('#btn_jhdw_start').unbind('click');
                $('#btn_jhdw_start').bind('click', function () {
                    changeJhdwState($.webPath() + '/organ/jhdw/start_jhdw', '确认启用此计划单位？');
                });
            } else {
                $("#btn_jhdw_start").text('停用');
                $("#btn_jhdw_start").removeClass('start').addClass('stop');
                $('#btn_jhdw_start').unbind('click');
                $('#btn_jhdw_start').bind('click', function () {
                    changeJhdwState($.webPath() + '/organ/jhdw/stop_jhdw', '确认停用此计划单位？');
                });
            }
            $("#btn_jhdw_start").show();
            jhdw.setBtnStatus();
        }*/
    };
    var columns = {columns: [
        [
            {field:'qc', title:'全称', width:50},
            {field:'jc', title:'简称', width:30},
            {field:'qyfr_qyqc', title:'企业法人', width:30},
            {field:'lxr', title:'联系人', width:30},
            {field:'bm', title:'编码', width:30},
            {field:'bz', title:'备注', width:60},
            {field:'sfzx', title:'启用', align:'center', width:10, formatter:function (value, row, index) {
                if (value == '是')
                    return '否';
                else
                    return '是';
            }}
        ]
    ]}
    options = $.extend(options, columns);
    return options;
}





